
import axios from 'axios'
import { errorDeal, getStore, globalLoading, setStore, successDeal } from '@/utils/utils'
import { showLoadingToast, closeToast, showToast } from 'vant';

axios.interceptors.request.use(function (config) {
    return config;
}, function (error) {

    return Promise.reject(error);
});

axios.interceptors.response.use(function (response) {
    return response;
}, function (error) {
    return Promise.reject(error);
});

/**
 *@describe http请求
 *@method {String} 请求方式
 *@url {String} 接口地址
 *@params {Object} 对应接口参数
 **/
export default async function _http(method = 'post', { url, apiName = '' }, params = {}, loading = true, headers = {}) {
    if (loading) {
        showLoadingToast({
            message: '请求中...',
            forbidClick: true,
            duration: 0,
        });
    }

    let boryaAuth = {}
    let Authorization = {}
    let json = {}
    let body = {}

    // Authorization = { Authorization: `'','','',3,20,''` }
    // json = {
    //     Token: `Bearer ${getStore('USER_INFO').token}`,
    //     platformHeadType: 'COMPANY_APP'
    // }
    body = {}

    return axios({
        method: method,
        url: '' + url,

        headers: Object.assign({
            'Content-Type': params.contentType ? 'multipart/form-data;' : 'application/json;charset=UTF-8;'
        }, json, boryaAuth ? Authorization : '', headers),
        params: method === 'get' ? params : '' || method === 'delete' ? params : '',
        data: params.contentType ? params.file : Object.assign(params, body),
        // data: typeof params === 'object' ? JSON.stringify(params) : params,
        timeout: 1000 * 60 * 2,
        responseType: params.responseType == 'blob' ? 'blob' : undefined,
        withCredentials: true
    }).then(res => {
        if (loading) {
            closeToast()
        }
        // 数据流，直接返回
        if (params.responseType == 'blob') {
            return res
        }

        if (res && res.data && res.data.code == 200) {
            return res.data
        } else throw res.data
    }).catch(error => {
        if (loading) {
            closeToast()
        }
        if (!params.closeErrorDeal) {
            errorDeal(error)
            throw error
        }
    })
}
